package org.springblade.mes.log.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.log.entity.SyncRecord;
import org.springblade.mes.log.mapper.SyncRecordMapper;
import org.springblade.mes.log.model.SyncRecordBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 服务
 *
 * @Version 1.0
 */
@Slf4j
@Service
public class SyncRecordService extends BaseServiceImpl<SyncRecordMapper, SyncRecord> {

    @Transactional(rollbackFor = Exception.class)
    public boolean addData(SyncRecordBean param) throws Exception {
        //保存
        SyncRecord entity = param;
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(SyncRecordBean param) throws Exception {
        //更新
        SyncRecord newEntity = param;
        return this.updateById(newEntity);
    }


    public void modifyData(SyncRecord record) throws Exception {
        if (record.getId() != null) {
            this.updateById(record);
        } else {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            record.setCreateUser(1L);
            record.setUpdateUser(1L);
            record.setStatus(0);
            this.save(record);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(SyncRecordBean param) throws Exception {
        if (param.getIdList() != null && param.getIdList().size() > 0) {
            UpdateWrapper<SyncRecord> del = new UpdateWrapper<>();
            del.lambda().in(SyncRecord::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }


    public SyncRecordBean findById(Long id) {
        SyncRecordBean cond = new SyncRecordBean();
        cond.setId(id);
        return findBySpec(cond);
    }


    public List<SyncRecordBean> findByBusinessType(String businessType) {
        SyncRecordBean cond = new SyncRecordBean();
        cond.setBusinessType(businessType);
        return findListBySpec(cond);
    }


    public SyncRecordBean findBySpec(SyncRecordBean param) {
        List<SyncRecordBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public List<SyncRecordBean> findListBySpec(SyncRecordBean param) {
        return this.baseMapper.queryList(param);
    }


    public IPage<SyncRecordBean> findPageBySpec(IPage<SyncRecordBean> page, SyncRecordBean param) {
        List<SyncRecordBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

}

